package cn.tedu.wenzhang2.controller;


import cn.tedu.wenzhang2.base.response.JsonResult;
import cn.tedu.wenzhang2.mapper.ArticleMapper;
import cn.tedu.wenzhang2.pojo.dto.ArticleDTO;
import cn.tedu.wenzhang2.pojo.entity.Article;
import cn.tedu.wenzhang2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("v1/article/")
@Api(tags = "文章模块")
public class ArticleController {
    @Autowired(required = false)
    private ArticleMapper articleMapper;


    @PostMapping("insert")
    @ApiOperation(value="发布文章")
    public JsonResult insertArticle(ArticleDTO articleDTO,@ApiIgnore HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        Article article=new Article();
        BeanUtils.copyProperties(articleDTO,article);
        article.setUserId(userVO.getId());
        int i = articleMapper.insertArticle(article);
        if (i==1)
            return JsonResult.ok();
        return JsonResult.notok();
    }



    @PostMapping("selectById")
    @ApiOperation(value="查询文章")
    public JsonResult selectById(Long id){
        return JsonResult.ok(articleMapper.selectById(id));
    }
}
